import  * as math from 'mathjs';

export class EnergyData {
    // 上月反向有功尖
    syfxygfl1;
    // 上月反向有功峰
    syfxygfl2;
    // 上月反向有功平
    syfxygfl3;
    // 上月反向有功谷
    syfxygfl4;
    // 上月反向有功总
    syfxygz;
    // 上月无功总
    sywgz;
    // 上月正向有功尖
    syzxygfl1;
    // 上月正向有功峰
    syzxygfl2;
    // 上月正向有功平
    syzxygfl3;
    // 上月正向有功谷
    syzxygfl4;
    // 上月正向有功总
    syzxygz;
    // 代收基金
    dsjj;
    // 力调系数
    ltxs;
    // 变压器容量
    byqrl;
    // 容量点价
    rldj;
    // 尖电价
    jdj;
    // 峰电价
    fdj;
    // 平电价
    pdj;
    // 无功铁损率
    wgtsl;
    // 有功铁损率
    ygtsl;
    // 本月反向有功尖
    byfxygfl1;
    // 本月反向有功峰
    byfxygfl2;
    // 本月反向有功平
    byfxygfl3;
    // 本月反向有功谷
    byfxygfl4;
    // 本月反向有功总
    byfxygz;
    // 本月无功总
    bywgz;
    // 本月正向有功尖
    byzxygfl1;
    // 本月正向有功峰
    byzxygfl2;
    // 本月正向有功平
    byzxygfl3;
    // 本月正向有功谷
    byzxygfl4;
    // 本月正向有功总
    byzxygz;
    // 每小时无功铁损
    mxswgts;
    // 每小时有功铁损
    mxsygts;
    // 电压变化
    dybh;
    // 电流变化
    dlbh;
    // 谷电价
    gdj;
    // 需量电价
    xldj;


    /**
     * 计算公式
     */
    calculationFormula = [
        "视在功率=（本月正向有功尖^2+本月正向有功峰^2）^0.5"
    ]

    /**
     * 计算字符串
     * @type {[string]}
     */
    mathStr = ["4 ^ 0.5"];
    /**
     * 计算结果
     * @type {[string]}
     */
    resStr = ["2"];

    constructor(syfxygfl1, syfxygfl2, syfxygfl3, syfxygfl4, syfxygz, sywgz, syzxygfl1, syzxygfl2, syzxygfl3, syzxygfl4, syzxygz, dsjj, ltxs, byqrl, rldj, jdj, fdj, pdj, wgtsl, ygtsl, byfxygfl1, byfxygfl2, byfxygfl3, byfxygfl4, byfxygz, bywgz, byzxygfl1, byzxygfl2, byzxygfl3, byzxygfl4, byzxygz, mxswgts, mxsygts, dybh, dlbh, gdj, xldj, calculationFormula=[]) {
        this.syfxygfl1 = syfxygfl1;
        this.syfxygfl2 = syfxygfl2;
        this.syfxygfl3 = syfxygfl3;
        this.syfxygfl4 = syfxygfl4;
        this.syfxygz = syfxygz;
        this.sywgz = sywgz;
        this.syzxygfl1 = syzxygfl1;
        this.syzxygfl2 = syzxygfl2;
        this.syzxygfl3 = syzxygfl3;
        this.syzxygfl4 = syzxygfl4;
        this.syzxygz = syzxygz;
        this.dsjj = dsjj;
        this.ltxs = ltxs;
        this.byqrl = byqrl;
        this.rldj = rldj;
        this.jdj = jdj;
        this.fdj = fdj;
        this.pdj = pdj;
        this.wgtsl = wgtsl;
        this.ygtsl = ygtsl;
        this.byfxygfl1 = byfxygfl1;
        this.byfxygfl2 = byfxygfl2;
        this.byfxygfl3 = byfxygfl3;
        this.byfxygfl4 = byfxygfl4;
        this.byfxygz = byfxygz;
        this.bywgz = bywgz;
        this.byzxygfl1 = byzxygfl1;
        this.byzxygfl2 = byzxygfl2;
        this.byzxygfl3 = byzxygfl3;
        this.byzxygfl4 = byzxygfl4;
        this.byzxygz = byzxygz;
        this.mxswgts = mxswgts;
        this.mxsygts = mxsygts;
        this.dybh = dybh;
        this.dlbh = dlbh;
        this.gdj = gdj;
        this.xldj = xldj;
        this.calculationFormula = calculationFormula;
    }

    // constructor(data) {
    //     this.syfxygfl1 = data.syfxygfl1 || null;
    //     this.syfxygfl2 = data.syfxygfl2 || null;
    //     this.syfxygfl3 = data.syfxygfl3 || null;
    //     this.syfxygfl4 = data.syfxygfl4 || null;
    //     this.syfxygz = data.syfxygz || null;
    //     this.sywgz = data.sywgz || null;
    //     this.syzxygfl1 = data.syzxygfl1 || null;
    //     this.syzxygfl2 = data.syzxygfl2 || null;
    //     this.syzxygfl3 = data.syzxygfl3 || null;
    //     this.syzxygfl4 = data.syzxygfl4 || null;
    //     this.syzxygz = data.syzxygz || null;
    //     this.dsjj = data.dsjj || null;
    //     this.ltxs = data.ltxs || null;
    //     this.byqrl = data.byqrl || null;
    //     this.rldj = data.rldj || null;
    //     this.jdj = data.jdj || null;
    //     this.fdj = data.fdj || null;
    //     this.pdj = data.pdj || null;
    //     this.wgtsl = data.wgtsl || null;
    //     this.ygtsl = data.ygtsl || null; // Assuming only one ygtsl field is needed
    //     this.byfxygfl1 = data.byfxygfl1 || null;
    //     this.byfxygfl2 = data.byfxygfl2 || null;
    //     this.byfxygfl3 = data.byfxygfl3 || null;
    //     this.byfxygfl4 = data.byfxygfl4 || null;
    //     this.byfxygz = data.byfxygz || null;
    //     this.bywgz = data.bywgz || null;
    //     this.byzxygfl1 = data.byzxygfl1 || null;
    //     this.byzxygfl2 = data.byzxygfl2 || null;
    //     this.byzxygfl3 = data.byzxygfl3 || null;
    //     this.byzxygfl4 = data.byzxygfl4 || null;
    //     this.byzxygz = data.byzxygz || null;
    //     this.mxswgts = data.mxswgts || null;
    //     this.mxsygts = data.mxsygts || null;
    //     this.dybh = data.dybh || null;
    //     this.dlbh = data.dlbh || null;
    //     this.gdj = data.gdj || null;
    //     this.xldj = data.xldj || null;
    // }

    map = {
        "上月反向有功尖": "syfxygfl1",
        "上月反向有功峰": "syfxygfl2",
        "上月反向有功平": "syfxygfl3",
        "上月反向有功谷": "syfxygfl4",
        "上月反向有功总": "syfxygz",
        "上月无功总": "sywgz",
        "上月正向有功尖": "syzxygfl1",
        "上月正向有功峰": "syzxygfl2",
        "上月正向有功平": "syzxygfl3",
        "上月正向有功谷": "syzxygfl4",
        "上月正向有功总": "syzxygz",
        "代收基金": "dsjj",
        "力调系数": "ltxs",
        "变压器容量": "byqrl",
        "容量点价": "rldj",
        "尖电价": "jdj",
        "峰电价": "fdj",
        "平电价": "pdj",
        "无功铁损率": "wgtsl",
        "有功铁损率": "ygtsl",
        "本月反向有功尖": "byfxygfl1",
        "本月反向有功峰": "byfxygfl2",
        "本月反向有功平": "byfxygfl3",
        "本月反向有功谷": "byfxygfl4",
        "本月反向有功总": "byfxygz",
        "本月无功总": "bywgz",
        "本月正向有功尖": "byzxygfl1",
        "本月正向有功峰": "byzxygfl2",
        "本月正向有功平": "byzxygfl3",
        "本月正向有功谷": "byzxygfl4",
        "本月正向有功总": "byzxygz",
        "每小时无功铁损": "mxswgts",
        "每小时有功铁损": "mxsygts",
        "电压变化": "dybh",
        "电流变化": "dlbh",
        "谷电价": "gdj",
        "需量电价": "xldj",
    }


    calculate() {
        this.mathStr = []
        this.resStr = []
        // 计算公式
        for (let i = 0; i < this.calculationFormula.length; i++) {
            try {
                let formula = this.calculationFormula[i];
                let nk = formula.replace(/(.*)=.*/, "$1")
                console.log(this.map)
                for (const k in this.map) {
                    formula = formula.replace(/.*=/, "")
                    formula = formula.replace("（", "(")
                    formula = formula.replace("）", ")")
                    // let partten = /k(?![\u4e00-\u9fa5])/
                    let partten = new RegExp(k + '(?![\u4e00-\u9fa5])')
                    console.log(partten)
                    formula = formula.replace(partten, this[this.map[k]])
                }
                this.mathStr.push(formula)
                // 计算结果
                let cuRes = math.evaluate(formula)
                this.resStr.push(cuRes.toFixed(2))
                // 更新map
                this.map[nk] = nk
                this[nk] = cuRes
            } catch (e) {
                console.log(e)
                let cuRes = 'err'
                this.resStr.push(cuRes)
            }
        }
        //  console.log(this.mathStr,this.resStr)
    }


}

// let energyData = new EnergyData(22,22)
// energyData.calculate();
// console.log(energyData.resStr)
// console.log(energyData.mathStr)

